package group2assignone.model;

import group2assignone.dao.MyDAO;
import group2assignone.ucm.UCStartup;

/**
 * models a booking
 * @author group2
 */
public class Booking {

    private TicketComponent ticket;
    boolean multipleFlights = false;
    int bookingNum;
    boolean booked = false;

    /**
     * Contructor, gets the DAO communication instance,
     * sets the bookingNumber.
     */
    public Booking() {
        MyDAO coms = UCStartup.getInstance().getDAO();
        this.bookingNum = coms.sQLGetLastBookingNumber() + 1;
        System.out.println("booking Number" + bookingNum);
    }

    /**
     * Adds the flight and username indicated by the parameters to the ticket.
     * @param flightNum
     * @param username
     */
    public void addFlight(String flightNum, String username) {
        if (ticket == null) //only need single ticket
        {
            ticket = new Ticket(flightNum, username);

        } else if (!multipleFlights) //need ticket composite
        {
            Ticket t = (Ticket) ticket; //store first ticket
            ticket = new TicketComposite(); //make composite
            ticket.addTicket(t); //add original ticket
            multipleFlights = true;

            Ticket newTick = new Ticket(flightNum, username); //make new ticket
            //If for same flight, add promotion
            if (ticket.containsFlight(flightNum)) {
                ticket.addTicket(new DiscountedTicket(newTick));
            } else {
                ticket.addTicket(newTick);
            }
        } else //already ticket composite -> just add flight
        {
            Ticket newTick = new Ticket(flightNum, username); //make new ticket

            //If for same flight, add promotion
            if (ticket.containsFlight(flightNum)) {
                ticket.addTicket(new DiscountedTicket(newTick));
            } else {
                ticket.addTicket(newTick);
            }
        }
    }

    /**
     * Adds the flight indicated by the parameter flightnum to the ticket.
     * @param flightNum
     */
    public void addFlight(String flightNum) {
        addFlight(flightNum, "Guest");

    }

    /**
     * Gets the booking number
     * @return
     */
    public int getBookingNum() {
        return bookingNum;
    }

    /**
     * Makes a booking with the ticket.
     * Returns a boolean indicating whether it was successful.
     * @return
     */
    public boolean makeBooking() {
        ticket.bookTicket(bookingNum, 1);
        booked = ticket.isBooked();
        return booked;
    }
}
